package com.xzmzhp.security;

import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.utils.return_tool.ResponseUtil;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * @author 王源(xm)
 * @Date: 2020/12/3 18:44
 * @Description: 登录失败处理类 返回500
 */
@Component
public class LoginFailureHandler implements Serializable, AuthenticationEntryPoint {
    private static final long serialVersionUID = -8970718410437077606L;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e)
            throws IOException {
        String msg = "认证失败，无法访问系统资源";
        ResponseUtil.out(response, ReturnData.error(ReturnConstant.AUTHENTICATION_FAILED, msg));
    }
}
